﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Collections.Generic;

namespace Xceed.Silverlight.Data.Stats
{
  public class ModeFunction : StatFunction
  {
    public ModeFunction()
      : base()
    {
    }

    public ModeFunction( string resultPropertyName, string sourcePropertyName )
      : base( resultPropertyName, sourcePropertyName )
    {
    }

    #region AllowMultimodal Property

    public bool AllowMultimodal
    {
      get
      {
        return m_allowMultimodal;
      }
      set
      {
        m_allowMultimodal = value;
      }
    }

    private bool m_allowMultimodal = false;

    #endregion

    protected internal override void InitializeFromInstance( StatFunction source )
    {
      base.InitializeFromInstance( source );
      this.AllowMultimodal = ( ( ModeFunction )source ).AllowMultimodal;
    }

    protected internal override void Validate()
    {
      base.Validate();
      this.ValidateSourcePropertyName( 1 );
    }

    protected internal override void Reset()
    {
      m_values.Clear();
    }

    protected internal override void Accumulate( object[] values )
    {
      if( StatFunction.CanProcessValues( values, 1 ) )
      {
        if( m_values.ContainsKey( values[ 0 ] ) )
        {
          m_values[ values[ 0 ] ]++;
        }
        else
        {
          m_values[ values[ 0 ] ] = 1;
        }
      }
    }

    protected internal override StatResult GetResult()
    {
      object result = null;
      int count = 1; // If all values have an count of 1, the Mode does not exist and we return nothing.

      if( m_allowMultimodal )
      {
        List<object> modes = new List<object>();

        foreach( KeyValuePair<object, int> entry in m_values )
        {
          if( entry.Value > count )
          {
            modes.Clear();
            modes.Add( entry.Key );
            count = entry.Value;
          }
          else if( ( entry.Value == count ) && ( count > 1 ) )
          {
            modes.Add( entry.Key );
          }
        }

        result = modes.ToArray();
      }
      else
      {
        foreach( KeyValuePair<object, int> entry in m_values )
        {
          if( entry.Value > count )
          {
            result = entry.Key;
            count = entry.Value;
          }
        }
      }

      return new StatResult( result );
    }

    protected internal override int GetEquivalenceKey()
    {
      return base.GetEquivalenceKey() ^ m_allowMultimodal.GetHashCode();
    }

    protected internal override bool IsEquivalent( StatFunction statFunction )
    {
      ModeFunction modeFunction = statFunction as ModeFunction;

      if( modeFunction == null )
        return false;

      if( modeFunction.AllowMultimodal != this.AllowMultimodal )
        return false;

      return base.IsEquivalent( statFunction );
    }

    private Dictionary<object, int> m_values = new Dictionary<object, int>();
  }
}
